<template>
    <div class="userpage">

        <div class="mian1">
            <div class="imgbox">
                <div class="imgblack"></div>
            </div>
            <div class="container">
                <div class="userbox" v-if="page.length!=0">
                    <div class="boxtop">
                        <div class="userhead">
                            <div class="imgbox">
                                <img :src="page.user.avatar.large" alt="" class="auto-img">
                            </div>
                        </div>
                        <div class="usercont">
                            <div class="top1">
                                <h2 style="color: #fff;">{{page.user.name}}</h2>
                                <ul>
                                    <li class="icon"><img src="../assets/img/China.svg" alt="" class="auto-img"></li>
                                    <li class="icon"><img src="../assets/img/pen.svg" alt="" class="auto-img"></li>
                                    <li class="icon"><img src="../assets/img/book.svg" alt="" class="auto-img"></li>
                                </ul>
                                <div class="likebox" @click="follow">
                                  <div class="liking" v-show="!page.care">关注</div>
                                  <div class="liking con" v-show="page.care">已关注</div>
                                </div>
                                <!-- <div class="skinny">
                                    <div class="icon">
                                        <img src="../assets/img/sixin.svg" alt="" class="auto-img">
                                    </div>
                                    <span style="color: #1088f2;">私信</span>
                                </div> -->
                            </div>
                            <div class="boxbtm">
                                <ul class="uesrdata">
                                    <li>
                                        <div class="num">1613</div>
                                        <div class="numname">声望</div>
                                    </li>
                                    <li>
                                        <div class="num">{{page.favorite_count}}</div>
                                        <div class="numname">粉丝</div>
                                    </li>
                                    <li>
                                        <div class="num">0</div>
                                        <div class="numname">关注</div>
                                    </li>
                                    <li>
                                        <div class="num">{{good}}</div>
                                        <div class="numname">被赞</div>
                                    </li>
                                    
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="qianm" style="margin-top: 24px; font-size: 14px;color: #838385;" >
                        这个人很懒什么都没留下
                    </div>
                </div>
            </div>
        </div>
        <div class="contentnav">
            <div class="container">
                <div class="warp1">
                    <div class="navbox">
                        <div class="title">
                            <h3>全部作品</h3>
                            <span>{{userdata.length}}</span>
                        </div>
                        <div class="iconbox">
                            <div class="iconlit">
                                <div class="icon">
                                    <img src="../assets/img/top1.svg" alt="" class="auto-img">
                                </div>
                                <span>0</span>
                            </div>
                            <div class="iconlit">
                                <div class="icon">
                                    <img src="../assets/img/top2.svg" alt="" class="auto-img">
                                </div>
                                
                                <span>0</span>
                            </div>
                            <div class="iconlit">
                                <div class="icon">
                                    <img src="../assets/img/top3.svg" alt="" class="auto-img">
                                </div>
                                <span>0</span>
                            </div>
                            <div class="iconlit">
                                <div class="icon">
                                    <img src="../assets/img/jingxuan.svg" alt="" class="auto-img">
                                </div>
                                <span>{{userdata.length}}</span>
                            </div>
                        </div>
                    </div>
                    <!-- <p style="font-size: 14px;">全部 ></p> -->
                </div>
                
            </div>
        </div>
        <div class="main" ref="main" v-if="userdata!=0">
            <div class="container">
                <div class="box">
                    <div class="content" @click="todetails(item.slug)" ref="waterfallItem" v-for="item in userdata" :key="item.slug">
                        <div class="imgbox">
                            <img :src="item.image.large" alt="">                    
                        </div>
                        <div class="boxmain">
                           <div class="user">
                            <p>{{item.title}}</p>
                            <div class="usermain">
                                <div class="useimg">
                                    <div class="headimg">
                                        <img class="auto-img" :src="item.user.avatar.large" alt="">
                                    </div>
                                    <p style="margin-left:10px;">{{item.user.name}}</p>
                                </div>
                                <div class="state">
                                    <div class="icon">
                                        <img src="../assets/img/goodw.png" alt="" class="auto-img">
                                    </div>
                                    <p>{{item.like_count}}</p>
                                    <div class="icon">
                                        <img src="../assets/img/news.png" alt="" class="auto-img">
                                    </div>
                                    <p>0</p>
                                </div>
                            </div>
                        </div> 
                        </div>
                        
                    </div>
                </div>
            </div>
        </div>
        <foot-navbar></foot-navbar>
    </div>
</template>

<script>

import FootNavbar from '@/components/FootNavbar.vue'
import { getLists } from '../api/lists'
import { getComment } from '../api/comment'
    export default {
        components: {  FootNavbar },
        data() {
            return {
                Lists: null,
                userdata:[],//用户发布图文数据
                Comment:[],
                good:0,
                page:[],
                followdata:[],
                array: [], //定义空数组存储元素高度
                height:[],//最大高度
                endindex:0,//最后一个元素的下标
                Bigheight:0,
                oneheight:[],
                };
        },
        methods:{
            // 关注
            follow(){
              // 判断是否登录
              let token=window.localStorage.getItem("token");
                if(!token){
                    this.$message('登陆后再试试吧~');
                    this.$router.push("/record")
                    return
                }
                if(!this.page.care){
                    //用户id
                    let id = this.page.user.slug;
                    // 遍历localstory 找到相同用户id发布的所有图文
                    this.Lists.forEach(item => {
                        if (item.user.slug == id) {
                            // 将id用户所有图文详情页的关注状态改变
                            item.care=true;
                            item.favorite_count+=1;
                        }
                    });
                    //去重
                    //寻找收藏数据id是否与当前页面数据id相等 
                    //找不到为-1表示收藏数据中不存在当前页面数据
                    var con=this.followdata.findIndex(item=>item.slug==this.page.slug);
                    if(con==-1){
                        this.followdata.push(this.page);
                        window.localStorage.setItem("followdata",JSON.stringify([this.followdata]));
                    }
                    this.$message.success('关注成功');
                    window.localStorage.setItem("Listsnew",JSON.stringify([this.Lists])); 
                }else if(this.page.care){
                    //用户id
                    let userid = this.page.user.slug;
                    // 遍历localstory 找到相同用户id发布的所有图文
                    this.Lists.forEach(item => {
                        if (item.user.slug == userid) {
                            // 将id用户所有图文详情页的关注状态改变
                            item.care=false;
                            item.favorite_count-=1;
                        }
                    });
                    //当前图文id
                    let id =this.$route.query.id;
                    // 找到当前id所在数组下标的数据
                    let index = this.followdata.findIndex((item)=>item.slug==id);
                    this.followdata.splice(index,1);
                    window.localStorage.setItem("followdata",JSON.stringify([this.followdata]));
                    this.$message('取消关注');
                    //将数据存入local story中
                    window.localStorage.setItem("Listsnew",JSON.stringify([this.Lists]));
                }
            },
            // 跳转到作者个人中心
            todetails(id){
                this.$router.push("/details?id="+ id +"");
            },
            //瀑布流
            getWaterfall() {
                let columns = 3; //定义布局的列数为2
                let item = this.$refs.waterfallItem; //获取每个子元素的DOM
                console.log("item",item)
                for (let i = 0; i < item.length; i++) {
                    //遍历整个子元素的DOM集合
                    if (i < columns) {
                    //小于columns的子元素作为第一行
                    item[i].style.top = 20 + 'px';
                    item[i].style.left = item[0].clientWidth * i + "px";
                    console.log("offsetWidth", item[0].clientHeight)
                    this.array.push(item[i].clientHeight); //遍历结束时，数组this.array保存的是第一行子元素的元素高度
                    let maxHeight = Math.max(...this.array); //  找到第一列的最小高度
                    let index = this.array.findIndex(item => item === maxHeight) // 找到最小高度的索引
                    this.oneheight.push(this.array[index]);
                    console.log(this.oneheight);
                    console.log("this.array",this.array)
                    } else {
                    //大于等于columns的子元素将作其他行
                    let minHeight = Math.min(...this.array); //  找到第一列的最小高度
                    let index = this.array.findIndex(item => item === minHeight) // 找到最小高度的索引
                    //设置当前子元素项的位置
                    item[i].style.top = this.array[index] +25+ "px"; 
                    item[i].style.left = item[index].offsetLeft + "px";
                    //重新定义数组最小项的高度 进行累加
                    this.array[index]+= item[i].clientHeight;
                    //最大高度
                    this.height.push(this.array[index]);
                    console.log("this.array[index]",this.array[index])
                    }
                }
                    this.endindex=this.height.length-1;
                    this.Bigheight=this.height[this.endindex];
                    if(this.userdata.length<=3){
                        this.$refs.main.style.height=(this.oneheight[0]+50+'px')
                    }
                    this.$refs.main.style.height=(this.Bigheight+50+'px')
                    console.log(this.Bigheight);
            },
            getListsFun(){
                // 接收用户id
                getLists({id:this.$route.query.id}).then(data=>{
                    // 所有数据
                    this.Lists=data.data.items;
                    if(window.localStorage.getItem("Listsnew")){
                        this.Lists=JSON.parse(window.localStorage.getItem("Listsnew"))[0];
                    }
                    // 用户id
                    let id = this.$route.query.id;
                    // 获取当前id用户下发布的所有图文数据
                    this.Lists.forEach(item => {
                        if (item.user.slug == id) {
                            // 将用户图文存取
                            this.userdata.push(item);
                            // 被赞总数
                            this.good+=item.like_count;
                        }
                    });
                    let index = this.Lists.findIndex(item=>item.user.slug==id);
                    this.page=this.Lists[index];
                })
            },
            // 接收评论区数据
            getCommentFun(){
                getComment().then(data=>{
                    this.Comment=data.data.items;
                    // 更新数据
                    if(window.localStorage.getItem("Comment")){
                        this.Comment=JSON.parse(window.localStorage.getItem("Comment"))[0];
                    }
                    if(window.localStorage.getItem("followdata")){
                        this.followdata=JSON.parse(window.localStorage.getItem("followdata"))[0];
                    }
                })
            }
        },
        created(){
            this.getListsFun();
            this.getCommentFun();
            this.$emit("login_local");
        setTimeout(() => { // 防止图片高度没有加载出来
            this.getWaterfall();
            },50)    
        },
        mounted(){
        },
       
    }
</script>

<style lang="scss" scoped>
.userpage {
  .mian1 {
    position: relative;
    & > .imgbox {
      height: 320px;
      background: url(../assets/img/userbackground.jpg) no-repeat;
      background-size: cover;
      background-position: 50%;
      margin-bottom: 144px;
    }
    .imgblack {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      background-color: rgba(0,0,0,.4);
    }
  }
  .main {
    padding-top: 20px;
    background-color: #f7f8f9;
    .container{
        position: relative;
    }
    .box {
    
    overflow: hidden;
      .content {
        cursor: pointer;
        width: 33%;
        height: auto;
        padding: 10px;
        margin: 0 0 10px 0;
        box-sizing: border-box;
        overflow: hidden;
        // margin-right: 5px;
        position: absolute;
        &:nth-child(3n){
            margin-right: 0;
        }
        .imgbox {
            overflow: hidden;
          img {
            width: 100%;
            height: 100%;
            // display: block;
          }
        }
      }
    }
    .content {
    //   position: relative;
    z-index: 1;
      overflow: hidden;
      margin-bottom: 16px;
      &:hover {
        .boxmain {
            transition: all 0.2s linear;
            opacity: 1;
        }
      }
      .imgbox{
        position: relative;
      }
      .boxmain{
        opacity: 0;
        .user {
        display: flex;
        flex-direction: column;
        justify-content: flex-end;
        background-image: linear-gradient(-180deg, transparent 0%, rgba(0, 0, 0, 0.6) 97%);
        padding: 16px;
        width: 95%;
        height: 95%;
        box-sizing: border-box;
        position: absolute;
        bottom: 14px;
        // left: -387px;
        left: 10px;
        p {
          font-size: 14px;
          font-weight: bold;
          color: #fff;
          margin-bottom: 10px;
        }
        .usermain {
          p {
            margin-bottom: 0;
          }
        }
      }
      }
      
    }
    .usermain {
      display: flex;
      justify-content: space-between;
      .headimg {
        width: 24px;
        height: 24px;
        border-radius: 50%;
        overflow: hidden;
      }
      .useimg {
        display: flex;
        align-items: center;
      }
      .state {
        display: flex;
        align-items: center;
        .icon {
          width: 16px;
          height: 16px;
          margin-right: 5px;
          margin-left: 10px;
          margin-top: -3px;
        }
      }
    }
  }
  .userbox {
    position: absolute;
    height: 160px;
    bottom: -105px;
  }
  .boxtop {
    display: flex;
    .userhead {
      margin-right: 24px;
      .imgbox {
        width: 120px;
        height: 120px;
        overflow: hidden;
        border-radius: 50%;
        border: 2px solid #fff;
      }
    }
    .usercont {
      .top1 {
        display: flex;
        align-items: center;
        .icon {
          width: 24px;
          height: 24px;
          margin: 0 5px;
        }
      }
    }
  }
  .usercont {
    display: flex;
    flex-direction: column;
    justify-content: center;
    .top1 {
      ul {
        display: flex;
        align-items: center;
        margin-right: 13px;
      }
      .liking {
        cursor: pointer;
        font-weight: bold;
        width: 64px;
        height: 32px;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #1088f2;
        border-radius: 5px;
        margin-right: 16px;
        font-size: 12px;
        color: #fff;
        &:hover{
          background-color: #1172c7;
        }
      }
      .con{
        background: #f7f7f7;
        border: 1px solid #ccc;
        box-sizing: border-box;
        color: #b0b0b0;
         &:hover{
          background-color: #f7f7f7;
        }
      }
      .skinny {
        cursor: pointer;
        width: 45px;
        padding: 0 20px;
        height: 32px;
        font-size: 14px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        background-color: #fff;
        border-radius: 5px;
        .icon {
          width: 16px;
          height: 16px;
          margin: 0;
        }
      }
    }
  }
  .boxbtm {
    .uesrdata {
      display: flex;
      width: 320px;
      justify-content: space-between;
      margin-top: 28px;
      li{
        text-align: center;
        transform: scale(0.95);
      }
    }
  }
  .uesrdata {
    .num {
      font-weight: bold;
    }
    .numname {
      margin-top: 8px;
      font-size: 14px;
      color: #838385;
    }
  }
  
    .contentnav {
    height: 60px;
    box-shadow: 0 0 3px #ccc;
    .navbox {
      height: 60px;
      display: flex;
      align-items: center;
      .title {
        display: flex;
        align-items: center;
        margin-right: 48px;
      }
      .iconbox {
        display: flex;
        .iconlit {
          display: flex;
          margin-right: 32px;
        }
        .icon {
          width: 24px;
          height: 24px;
        }
      }
    
  }
  .navbox {
    span {
      font-size: 17px;
      margin-left: 8px;
    }
  }
  .warp1 {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  }
  /* 瀑布流布局 */
  


}


</style>